{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "78a28c77-ba1c-48a6-a577-53ad39e900a0",
   "metadata": {},
   "source": [
    "Chapter 08\n",
    "# 自定义函数中使用**kwargs\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "de81426a-baba-418f-9f2d-e0b6faa98cc1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 利用*kwargs\n",
    "def multiply_all_2(**kwargs):\n",
    "    result = 1\n",
    "    print(type(kwargs))\n",
    "    # 循环dict()\n",
    "    for key, value in kwargs.items():\n",
    "        print(\"The value of {} is {}\".format(key, value))\n",
    "        result *= value\n",
    "        \n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f5a3f5f5-a2ad-465a-9258-96a587cf2620",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "The value of A is 1\n",
      "The value of B is 2\n",
      "The value of C is 3\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "# 计算3个key-value pairs中值的乘积\n",
    "print(multiply_all_2(A = 1, B = 2, C = 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7bd7013b-7976-4ec5-a8a5-43e528b191a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "The value of A is 1\n",
      "The value of B is 2\n",
      "The value of C is 3\n",
      "The value of D is 4\n",
      "24\n"
     ]
    }
   ],
   "source": [
    "# 计算4个key-value pairs中值的乘积\n",
    "print(multiply_all_2(A = 1, B = 2, C = 3, D = 4))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
